home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxfrac.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  3.4 KB  |  93 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxfrac.h,v 1.2 2000/09/19 19:00:37 lpd Exp $ */
  20. /* Fraction representation for Ghostscript */
  21.  
  22. #ifndef gxfrac_INCLUDED
  23. #  define gxfrac_INCLUDED
  24.  
  25. /*
  26.  * Represent a fraction in [0.0..1.0].
  27.  * Note that the 1.0 endpoint is included.
  28.  * Since undercolor removal requires a signed frac,
  29.  * we limit fracs to 15 bits rather than 16.
  30.  */
  31. typedef short frac;
  32. typedef short signed_frac;
  33.  
  34. #define arch_log2_sizeof_frac arch_log2_sizeof_short
  35. #define arch_sizeof_frac arch_sizeof_short
  36. #define frac_bits 15
  37. #define frac_0 ((frac)0)
  38. /* The following definition of frac_1 allows exact representation of */
  39. /* almost all common fractions (e.g., N/360 for 0<=N<=360). */
  40. #define frac_1_0bits 3
  41. #define frac_1 ((frac)0x7ff8)
  42. #define frac_1_long ((long)frac_1)
  43. #define frac_1_float ((float)frac_1)
  44. /* Conversion between fracs and floats. */
  45. #define frac2float(fr) ((fr) / frac_1_float)
  46. #define float2frac(fl) ((frac)(((fl) + 0.5 / frac_1_float) * frac_1_float))
  47.  
  48. /*
  49.  * Conversion between unsigned fracs and bytes (or, in general,
  50.  * shorter integers) representing fractions. This is highly dependent
  51.  * on the definition of frac_1 above.
  52.  */
  53. #define _frac2s(fr)\
  54.   (((fr) >> (frac_bits - frac_1_0bits)) + (fr))
  55. #define frac2bits(fr, nb)\
  56.   ((uint)(_frac2s(fr) >> (frac_bits - (nb))))
  57. #define frac2byte(fr) ((byte)frac2bits(fr, 8))
  58. /* bits2frac requires frac_bits / 2 <= nb <= frac_bits. */
  59. #define bits2frac(v, nb) ((frac)(\
  60.   ((frac)(v) << (frac_bits - (nb))) +\
  61.    ((v) >> ((nb) * 2 - frac_bits)) -\
  62.    ((v) >> ((nb) - frac_1_0bits)) ))
  63. #define byte2frac(b) bits2frac(b, 8)
  64. /* Produce a result that is guaranteed to convert back to a frac */
  65. /* not exceeding the original value fr. */
  66. #define frac2bits_floor(fr, nb)\
  67.   ((uint)((_frac2s(fr) - (_frac2s(fr) >> (nb))) >> (frac_bits - (nb))))
  68. /*
  69.  * Conversion between fracs and unsigned shorts.
  70.  */
  71. #define ushort_bits (arch_sizeof_short * 8)
  72. #define frac2ushort(fr) ((ushort)(\
  73.   ((fr) << (ushort_bits - frac_bits)) +\
  74.   ((fr) >> (frac_bits * 2 - ushort_bits - frac_1_0bits)) ))
  75. #define ushort2frac(us) ((frac)(\
  76.   ((us) >> (ushort_bits - frac_bits)) -\
  77.   ((us) >> (ushort_bits - frac_1_0bits)) ))
  78. /*
  79.  * Compute the quotient Q = floor(P / frac_1),
  80.  * where P is the (ulong) product of a uint or ushort V and a frac F.
  81.  * See gxarith.h for the underlying algorithm.
  82.  */
  83. #define frac_1_quo(p)\
  84.   ( (((p) >> frac_1_0bits) + ((p) >> frac_bits) + 1) >> (frac_bits - frac_1_0bits) )
  85. /*
  86.  * Compute the remainder similarly, having already computed the quotient.
  87.  * This is, of course, P - Q * frac_1.
  88.  */
  89. #define frac_1_rem(p, q)\
  90.   ((frac)( (uint)(p) - ((q) << frac_bits) + ((q) << frac_1_0bits) ))
  91.  
  92. #endif /* gxfrac_INCLUDED */
  93.